package com.school.graduation.goods.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.goods.presenter.FoundGoodPresenter;
import com.school.graduation.util.NetWorkUtil;
import com.school.graduation.util.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

public class FoundGoodsActivity extends BaseActivity{

    @Bind(R.id.found_good_quit)
    ImageView foundGoodQuit;
    @Bind(R.id.found_good_small_down)
    ImageView foundGoodSmallDown;
    @Bind(R.id.found_good_edit)
    EditText foundGoodEdit;
    @Bind(R.id.found_good_rectangle)
    RelativeLayout foundGoodRectangle;
    @Bind(R.id.found_good_find)
    TextView foundGoodFind;
    @Bind(R.id.find_good_dustbin)
    ImageView findGoodDustbin;
    @Bind(R.id.find_good_history_find_layout)
    RelativeLayout findGoodHistoryFindLayout;
    @Bind(R.id.put_of_symbal)
    ImageView putOffSymbal;

    private FoundGoodPresenter presenter;

    public static void jumpToFoundActivity(Context context, Bundle bundle) {
        Intent intent = new Intent(context, FoundGoodsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_found_good;
    }

    @Override
    public void initData() {
        presenter = new FoundGoodPresenter(context);
        //initLabel();
        setEdittextListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //presenter.getHasFoundedHistory();
        foundGoodEdit.setText("");
    }

    private void setEdittextListener() {
        foundGoodEdit.addTextChangedListener(new MyTextWatcher());
    }

    @OnClick({R.id.found_good_quit, R.id.found_good_find, R.id.find_good_dustbin,R.id.put_of_symbal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.found_good_quit:
                finish();
                break;
            case R.id.found_good_find:
                String date = foundGoodEdit.getText().toString();
                if (date.equals("")) ToastUtil.makeShortToast(context,"搜索关键词不能为空");
                else if (!NetWorkUtil.isNetWorkConnected(context)) ToastUtil.makeShortToast(context,"请检查网络状态");
                else presenter.queryDateFromDate(date);
                break;
            case R.id.put_of_symbal:
                foundGoodEdit.setText("");
                break;
            case R.id.find_good_dustbin:
                presenter.showToastDialog();
                break;
        }
    }

    class MyTextWatcher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count!=0) putOffSymbal.setVisibility(View.VISIBLE);
            if (count==0&&start==0) putOffSymbal.setVisibility(View.GONE);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
